package com.microservice.microservicemovie.controller;

import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.EurekaClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * 〈一句话功能简述〉
 * 〈功能详细描述〉
 *
 * @author bf
 * @create 2017/11/7
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */

@RestController
@RequestMapping
public class MovieController {

    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private LoadBalancerClient loadBalancerClient;

    @GetMapping(value = {"/movie/{id}"})
    public String findById(@PathVariable Long id){

        ServiceInstance choose1 = loadBalancerClient.choose("microservice-user");
        System.out.println("111111:" + choose1.getServiceId() + ":" + choose1.getHost() + ":" + choose1.getPort());

        // 跨服务调用 localhost:7902 --> 改为 spring.application.name 方式的 访问
        // microservice-user --> VIP virtual IP
        return restTemplate.getForObject("http://microservice-user/user/" + id, String.class);
        /**
         *  跨服务调用，若出现 No instances available for localhost 错误，就是在 RestTemplate 中加过了这个注解 @LoadBalanced，
         *  需要使用 spring.application.name 进行访问，所以并没有访问到
         */
    }


    /**
     *  测试数据：microservice-user 使用我们自定义的规则，随机
     *            microservice-user2 任然使用默认的，负载均衡的
     *      111111:microservice-user:localhost:7904
     *      22222:microservice-user2:localhost:7905
     *      111111:microservice-user:localhost:7904
     *      22222:microservice-user2:localhost:7906
     *      111111:microservice-user:localhost:7904
     *      22222:microservice-user2:localhost:7905
     *      111111:microservice-user:localhost:7902
     *      22222:microservice-user2:localhost:7906
     *      111111:microservice-user:localhost:7904
     *      22222:microservice-user2:localhost:7905
     *      111111:microservice-user:localhost:7904
     *      22222:microservice-user2:localhost:7906
     *      111111:microservice-user:localhost:7904
     *      22222:microservice-user2:localhost:7905
     *      111111:microservice-user:localhost:7902
     *      22222:microservice-user2:localhost:7906
     * @return
     */
    @GetMapping(value = {"/test"})
    public String test(){
        ServiceInstance choose1 = loadBalancerClient.choose("microservice-user");
        System.out.println("111111:" + choose1.getServiceId() + ":" + choose1.getHost() + ":" + choose1.getPort());

        ServiceInstance choose2 = loadBalancerClient.choose("microservice-user2");
        System.out.println("22222:" + choose2.getServiceId() + ":" + choose2.getHost() + ":" + choose2.getPort());

        return "test";
    }
}
